<template>
  <div>
    <el-form :model="user" :rules="rules">
      <el-form-item label="请输入手机号" prop="mobile">
        <el-input v-model="user.mobile" />
      </el-form-item>
      <el-form-item label="请输入密码" prop="password">
        <el-input v-model="user.password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="password2">
        <el-input v-model="user.password2" />
      </el-form-item>
      <el-button>登入</el-button>
    </el-form>
  </div>
</template>

<script>
import { userPassword2, valMobile } from '../../utils/validate'
export default {
  data() {
    //   自定义校验函数
    // 验证二次密码一致
    const checkpassword = (rule, val, callback) => {
      // 触发校验，函数就会被触发
      // 自动传入三个函数
      // rule 当前规则，不用也不能删除
      // val  当前输入的值
      // callback   放行回调函数
      //
    //   const isSamePass = val === this.user.password
      userPassword2(val, this.user.password, callback)
    }
    const userMobile = (rule, val, callback) => {
      valMobile(val, callback)
    }
    return {
      user: {
        mobile: '',
        password: '',
        password2: ''

      },
      rules: {
        mobile: [
          // required: true  输入框必写事件   trigger: 'blur'  失焦事件
          { required: true, message: '内容不能为空', trigger: 'blur' },
          { validator: userMobile, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '内容不能为空', trigger: 'blur' },
          { min: 6, max: 12, message: '请输入6到12位密码', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '内容不能为空', trigger: 'blur' },
          { validator: checkpassword, trigger: 'blur' }
        ]
      }
    }
  }
}
</script>

<style>

</style>
